package com.sihai.ehr.web.action.workno;

import com.sihai.core.LimitedList;
import com.sihai.core.beans.BeanInject;
import com.sihai.ehr.model.biz.WorkNo;
import com.sihai.ehr.model.sys.Organization;
import com.sihai.ehr.service.OrganizationService;
import com.sihai.ehr.service.WorkNoService;
import com.sihai.ehr.web.action.EHRAction;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

/**
 * @author wxn
 * @since 12-6-24 13:21
 */

@Results({
        @Result(name = "list", location = "list.jsp"),
        @Result(name = "main", location = "main.jsp")
})
public class WorkNoAction  extends EHRAction {
    @BeanInject
    private WorkNoService workNoService;

    @BeanInject
    private OrganizationService organizationService;

    private WorkNo workNo;

    @Action("/workno/main")
    @Override
    public String execute() throws Exception {
        listAction = "/workno/list.xhtml";
        listTitle = "工号规则设置";
        return "main";
    }

    @Action("/workno/list")
    public String list() throws Exception {
        LimitedList<WorkNo> workNos = workNoService.findByOrganization(organizationId, createLimit(), createSorter());
        if(workNos != null && !workNos.isEmpty()){
            workNo = workNos.get(0);
        }

        if(workNo == null){
            workNo = new WorkNo();
            Organization o = organizationService.get(organizationId);
            workNo.setOrganization(o);
        }
        return "list";
    }


    public WorkNo getWorkNo() {
        return workNo;
    }

    public void setWorkNo(WorkNo workNo) {
        this.workNo = workNo;
    }
}
